package com.sdy.hospitalguard.h5.controller;

import com.sdy.common.model.BizException;
import com.sdy.common.model.Response;
import com.sdy.common.utils.Assert;
import com.sdy.common.utils.IdCardUtil;
import com.sdy.hospitalguard.biz.dto.PersonBaseDataDTO;
import com.sdy.hospitalguard.biz.model.HospitalUser;
import com.sdy.hospitalguard.biz.service.ApplyPostService;
import com.sdy.hospitalguard.biz.service.HospitalUserService;
import com.sdy.mvc.controller.BaseController;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;

/**
 * @author: 王越洋
 * @version: v1.0
 * @description: com.sdy.hospitalguard.h5.controller
 * @date:2020/5/9
 */
@Slf4j
@RestController
@Api(tags = "用户注册接口")
public class HospitalUserRegisterController extends BaseController {
    @Autowired
    private HospitalUserService hospitalUserService;
    @Autowired
    private ApplyPostService applyPostService;

    @PostMapping("/userRegister")
    @ApiOperation(value = "用户注册", response = HospitalUser.class)
    public Response userRegister(@RequestBody HospitalUser hospitalUser, HttpServletRequest request) throws Exception {
        Assert.isNull(hospitalUser.getIdCard(), "身份证为空");
        Integer userId = getUserId(request);
        Assert.isNull(userId, "当前用户为空");
        checkData(hospitalUser);
        HospitalUser byId = hospitalUserService.getById(userId);
        Assert.isNull(byId, "数据库中未存储,此用户ID");
        Boolean aBoolean = hospitalUserService.userRegister(hospitalUser, userId);
        return Response.success(aBoolean);
    }

    @GetMapping("/getMyBaseDate")
    @ApiOperation(value = "我的基本信息", response = PersonBaseDataDTO.class)
    public Response getMyBaseDate(HttpServletRequest request) throws BizException {
        Integer userId = getUserId(request);
        Assert.isNull(userId, "用户未登陆!");
        // 我的基本信息
        // 此人目前的身份(0-无直接关系 1-患者 2-陪护 3-探视人员 4-陪护+探视)
        Integer identity = applyPostService.checkIdentity(userId);
        HospitalUser byId = hospitalUserService.getById(userId);
        PersonBaseDataDTO personBaseDataDTO = new PersonBaseDataDTO()
                .setId(byId.getId())
                .setName(byId.getName())
                .setIdCard(byId.getIdCard())
                .setPhone(byId.getPhone())
                .setIdentity(identity);
        if (StringUtils.isNotBlank(byId.getPhotoPath())) {
            personBaseDataDTO.setPhotoPath(byId.getPhotoPath()).setUuid(byId.getHeadPortrait());
        }
        return Response.success(personBaseDataDTO);
    }

    public static void checkData(HospitalUser hospitalUser) throws BizException {
        Assert.isTrue(StringUtils.isBlank(hospitalUser.getName()), "请规范填写姓名!");
        Assert.isTrue(!IdCardUtil.IDCardValidate(hospitalUser.getIdCard()).equals("true"), "身份证格式错误");
        Assert.isTrue(!hospitalUser.getPhone().matches(com.sdy.common.constant.Constants.REGEX_PHONE), "手机号格式错误");
    }

}
